package com.yh.modules.data.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.common.utils.Assert;
import com.yh.common.utils.PageUtils;
import com.yh.common.utils.Query;
import com.yh.modules.data.dao.MachineErrorInfoDao;
import com.yh.modules.data.entity.JqGridEntity;
import com.yh.modules.data.entity.MachineEntity;
import com.yh.modules.data.entity.MachineErrorInfoEntity;
import com.yh.modules.data.service.MachineErrorInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: skh
 * @Date: 2020/3/23 15:07
 * @Description:
 */
@Service
@Slf4j
public class MachineErrorInfoServiceImpl extends ServiceImpl<MachineErrorInfoDao, MachineErrorInfoEntity> implements MachineErrorInfoService {

    @Autowired
    private MachineServiceImpl machineService;

    @Override
    public void saveMachineErrorReason(MachineErrorInfoEntity machineErrorInfoEntity) {
        this.insert(machineErrorInfoEntity);
    }

    @Override
    public PageUtils getErrorInfoList(Map<String, Object> params) {
        Integer machineId = MapUtil.getInt(params, "machineId");
        Assert.isNull(machineId, "机器id不能为空");
        String startTime = MapUtil.getStr(params, "startTime");
        String endTime = MapUtil.getStr(params, "endTime");

        EntityWrapper<MachineErrorInfoEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("machine_id", machineId);
        wrapper.ge(StrUtil.isNotBlank(startTime), "create_time", startTime);
        wrapper.le(StrUtil.isNotBlank(endTime), "create_time", endTime);
        wrapper.orderBy("create_time", false);
        Page<MachineErrorInfoEntity> page = this.selectPage(new Query<MachineErrorInfoEntity>(params).getPage(), wrapper);

        return new PageUtils(page);
    }

    @Override
    public JqGridEntity getErrorColNamesAndModel(Integer machineId) {

        List<String> colNameList = CollUtil.newArrayList("记录id",  "数据上传时间", "暂停时间(秒)","错误类型编号");
        //List<String> errorTypeNameList = errorTypeList.stream().map(ErrorTypeEntity::getDescription).collect(Collectors.toList());
        //colNameList.addAll(errorTypeNameList);
        log.debug("表头列表:{}", colNameList);

        List<String> colModelList = CollUtil.newArrayList("id",  "createTime", "pauseTime","errorTypeNum");
        //List<String> errorTypeModelList = errorTypeList.stream().map(errorTypeEntity -> "errorType" + errorTypeEntity.getId()).collect(Collectors.toList());
        //colModelList.addAll(errorTypeModelList);
        log.debug("模型列表:{}", colModelList);

        //List<Integer> sortList = errorTypeList.stream().map(ErrorTypeEntity::getSort).collect(Collectors.toList());

        //根据colModelList拼装colModel
        //{ label: '项目id', name: 'id', index: 'id', width: 50,sortable: false, key: true },
        List<Map<String, Object>> colModelEntity = assembleJqidColModelList(colNameList, colModelList);
        log.debug("colModel实体:{}", colModelEntity);

        MachineEntity machineEntity = machineService.selectById(machineId);


        JqGridEntity jqGridEntity = new JqGridEntity();
        jqGridEntity.setColNameList(colNameList);
        jqGridEntity.setColModelList(colModelList);
        jqGridEntity.setColModelEntity(colModelEntity);
        jqGridEntity.setMachineEntity(machineEntity);
        return jqGridEntity;
    }

    private List<Map<String, Object>> assembleJqidColModelList(List<String> colNameList, List<String> colModelList) {
        List<Map<String, Object>> colModelEntity = new ArrayList<>();
        for (int i = 0; i < colNameList.size(); i++) {
            Map<String, Object> colModel = new HashMap<>();
            colModel.put("label", colNameList.get(i));
            colModel.put("name", colModelList.get(i));
            colModel.put("sortable", false);
            colModelEntity.add(colModel);
        }
        return colModelEntity;
    }
}
